﻿
@model ShenNius.Share.Models.Entity.Cms.AdvList
@using ShenNius.Share.Models.Enums.Cms;
@using ShenNius.Share.Models.Enums.Extension;
    @section css{
        <style>
            .layui-form-item .layui-input {
                width: 350px;
                padding-right: 10px;
                line-height: 38px;
            }

            .layui-form-item .layui-textarea {
                width: 350px;
            }

            .layui-form-select {
                width: 350px;
            }

            .layui-upload-img {
                width: 135px;
                height: 95px;
                padding-left: 150px;
            }
        </style>
    }
    <form class="layui-form" action="" lay-filter="advList-edit">
        <div class="layuimini-container layuimini-page-anim">
            <div class="layuimini-main">
                <div class="layui-form layuimini-form">
                    @if (Model.Id > 0)
                    {
                        <div class="layui-form-item">
                            <label class="layui-form-label">编号</label>
                            <div class="layui-input-block">
                                <input type="text" name="id" lay-verify="id" value="@Model.Id" disabled class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label required">添加时间</label>
                            <div class="layui-input-block">
                                <input type="text" name="createTime" value="@Model.CreateTime.ToWebString()" readonly class="layui-input">
                            </div>
                        </div>
                    }
                    <div class="layui-form-item">
                        <label class="layui-form-label required">类型</label>
                        <div class="layui-input-block">
                            <select name="type" id="parentId" lay-verify="parentId" lay-filter="parentId">
                                @foreach (var item in (Dictionary<int, string>)ViewBag.Dic)
                                {
                                    if (item.Key == (int)Model.Type)
                                    {
                                        <option value="@item.Key" selected>@item.Value</option>
                                    }
                                    else
                                    {
                                        <option value="@item.Key">@item.Value</option>
                                    }
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">广告名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" lay-verify="required" value="@Model.Title" lay-reqtext="栏目名称不能为空" placeholder="请输入栏目名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">图片</label>
                        <div class="layui-upload">
                            <button type="button" class="layui-btn" id="test1">上传图片</button>
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" src="@Model.ImgUrl" id="demo1">
                                <p id="demoText"></p>
                            </div>
                            <input type="hidden" id="imgUrl" value="@Model.ImgUrl" name="imgUrl" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label ">链接地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="LinkUrl"  value="@Model.LinkUrl"  class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">排序</label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" value="@Model.Sort" lay-verify="required" l class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label ">描述信息</label>
                        <div class="layui-input-block">
                            <textarea name="summary"  value="@Model.Summary" class="layui-textarea" placeholder="请输入描述信息"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    @section js{
        <script>

        layui.use(['form', 'common','upload'], function () {
            var form = layui.form,
                upload = layui.upload,
                $ = layui.jquery,
                apiUtil = layui.common;
            /**
             * 初始化表单，要加上，不然刷新部分组件可能会不加载
             */
            // 当前弹出层，防止ID被覆盖
            var parentIndex = parent.layer.getFrameIndex(window.name);

            var id =@Model.Id;
            form.render();

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: apiUtil.apiUrl() +'advList/qiniuFile' //改成您自己的上传接口
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    if (res.statusCode == 200 && res.success == true) {
                        $('#demo1').attr('src', res.data);
                        $('#imgUrl').val(res.data);
                        console.log("img:" + res.data);
                    }
                    //上传成功
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
               });

                //监听提交
                form.on('submit(saveBtn)', function (data) {
                    if (data.field.id>0) {
                        apiUtil.ajax('advList/modify', data.field, "application/json", "put", function (res) {
                            if (res.statusCode == 200 && res.success == true) {
                                apiUtil.success(res.msg);
                                parent.layer.close(parentIndex);
                            } else {
                                apiUtil.error(res.msg);
                            }
                        });
                    } else {
                        apiUtil.ajax('advList/add', data.field, "application/json", "post", function (res) {
                            if (res.statusCode == 200 && res.success == true) {
                                apiUtil.success(res.msg);
                                parent.layer.close(parentIndex);
                            } else {
                                apiUtil.error(res.msg);
                            }
                        });
                    }
                    return false;
                });
            });

        </script>

    }
